package com.qf.shop.dao.Impl;

import com.qf.shop.dao.OrdersDao;
import com.qf.shop.entity.Address;
import com.qf.shop.entity.Goods;
import com.qf.shop.entity.Orders;
import com.qf.shop.entity.OrdersDetail;
import com.qf.shop.utils.DruidUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrdersDaoImpl implements OrdersDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public int insertOrders(Orders orders) {
        Object[] params = {orders.getId(), orders.getUid(), orders.getMoney(), orders.getStatus(), orders.getTime(), orders.getAid()};
        try {
            return queryRunner.update(DruidUtils.getConnection(), "insert into orders(id,uid,money,status,time,aid)values(?,?,?,?,?,?)", params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    //级联生成的订单详情
    @Override
    public void insertOrdersDetail(List<OrdersDetail> ordersDetails) {
        //1.创建二维数组
        Object[][] params = new Object[ordersDetails.size()][];
        //2.固定一个sql语句
        String sql = "insert into ordersdetail(gid,oid,num,money) value(?,?,?,?) ";
        for (int i = 0; i < ordersDetails.size(); i++) {
            //没循环一次，获取一条订单详情对象
            OrdersDetail ordersDetail = ordersDetails.get(i);
            //每个订单详情对象对应一条sql语句参数的赋值
            params[i] = new Object[]{ordersDetail.getGid(), ordersDetail.getOid(), ordersDetail.getNum(), ordersDetail.getMoney()};
        }
        try {
            queryRunner.batch(DruidUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Orders> selectOrdersByUid(int uid) {
        List<Orders> ordersList = null;
        try {////-----------------------sql语句

            List<Map<String, Object>> maps = queryRunner.query(DruidUtils.getConnection(), "select od.id,od.uid,od.money,od.status,od.time,od.aid,\n"
                    + " ad.detail,ad.name,ad.phone,ad.uid,ad.level from orders as od\n" +
                    " inner join address as ad \n " +
                    "on od .aid = ad.id where od.uid = ?;", new MapListHandler(), uid);
            //MAP --->    key,value    列名,列值
            //List--->  每行数据
            if (maps == null) {
                return null;
            }
            //一个 orders 的uid对应多个订单
            ordersList = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                //完成了一个订单的获取和一个订单的封装
                Orders orders = new Orders();
                Address address = new Address();
                //工具类封装对象
                BeanUtils.populate(orders, map);
                BeanUtils.populate(address, map);
                orders.setAddress(address);
                ordersList.add(orders);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return ordersList;
    }

    @Override
    public Orders selectOrdersByOid(String oid) {
        Orders orders = null;
        try {
            Map<String, Object> maps = queryRunner.query(DruidUtils.getConnection(), "select od.id, od.uid, od.money, od.status, od.time, od.aid,\n" +
                    "       ad.detail, ad.name, ad.phone,ad.uid, ad.level from orders as od\n" +
                    "inner join address as ad\n" +
                    "on od.aid = ad.id where od.id = ?;", new MapHandler(), oid);
            //MAP --->    key,value    列名,列值
            if (maps == null) {
                return null;
            }
            //一个uid对应有多个订单
            orders = new Orders();
            Address address = new Address();
            BeanUtils.populate(orders, maps);
            BeanUtils.populate(address, maps);
            orders.setAddress(address);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return orders;
    }

    @Override
    public List<OrdersDetail> selectOrdersDetailByOid(String oid) {
        List<OrdersDetail> ordersDetails = null;
        try {
            List<Map<String, Object>> maps = queryRunner.query(DruidUtils.getConnection(), "select od.gid, od.oid, od.num, od.money,\n" +
                    "        gs.name,  gs.pubdate,  gs.picture,  gs.price,  gs.star,  gs.info,  gs.typeid\n" +
                    "from ordersdetail as od\n" +
                    "inner join goods as gs\n" +
                    "on gs.id = od.gid where oid = ?;", new MapListHandler(), oid);
            if (maps == null) {
                return null;
            }
            ordersDetails = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                OrdersDetail ordersDetail = new OrdersDetail();
                Goods goods = new Goods();
                BeanUtils.populate(ordersDetail, map);
                BeanUtils.populate(goods, map);
                ordersDetail.setGoods(goods);
                ordersDetails.add(ordersDetail);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return ordersDetails;
    }
        @Override
        public int updateOrders (String oid){
            try {
                return queryRunner.update(DruidUtils.getConnection(), "update orders set status = 2 where id = ?;",oid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }

